package com.mission.game.otherscreens;

import java.util.Calendar;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mission.game.R;
import com.mission.game.utils.BarcodeTask;
import com.mission.game.utils.GPSTask;
import com.mission.game.utils.Game;
import com.mission.game.utils.Mission;
import com.mission.game.utils.PictureTask;
import com.mission.game.utils.Task;
import com.mission.game.utils.TextTask;
import com.mission.game.utils.User;

public class LoginScreen extends Activity {
	private static final int PLAY = 0;

	private HashMap<String, User> _users;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		_users = new HashMap<String, User>();
		setContentView(R.layout.login_screen);
	}

	public void onLogin(View v) {
		String user = ((EditText) findViewById(R.id.user_input)).getText()
				.toString().trim().toLowerCase();
		if (user.isEmpty()) {
			Toast.makeText(getApplicationContext(), R.string.msg_name_required,
					Toast.LENGTH_LONG).show();
			return;
		}

		String pass = ((EditText) findViewById(R.id.password_input)).getText()
				.toString();
		if (pass.isEmpty()) {
			Toast.makeText(getApplicationContext(), R.string.msg_pass_required,
					Toast.LENGTH_LONG).show();
			return;
		}

		if (!_users.containsKey(user))
			_users.put(user, createExampleUser(user, pass));

		if (!pass.equals(_users.get(user).getPass())) {
			Toast.makeText(getApplicationContext(), R.string.msg_wrong_pass,
					Toast.LENGTH_LONG).show();
			return;
		}

		Intent game = new Intent(getApplicationContext(), MainScreen.class);
		game.putExtra("user", _users.get(user));
		startActivityForResult(game, PLAY);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null && data.getExtras() != null
				&& data.getExtras().containsKey("user")) {
			User user = (User) data.getExtras().get("user");
			_users.put(user.getName().trim().toLowerCase(), user);
		}
		((EditText) findViewById(R.id.user_input)).setText("");
		((EditText) findViewById(R.id.password_input)).setText("");
		((EditText) findViewById(R.id.user_input)).requestFocus();
	}

	private User createExampleUser(String name, String pass) {
		User user = new User(name, pass);
		Game game1 = new Game("Missions in Jerusalem",
				"Run around Jerusalem, completing fun missions!",
				Calendar.getInstance(), false, Game.Visibility.PUBLIC);

		String[] easyMissions = { "High-five a stop sign.",
				"Wash a street crossing.", };
		String[] unpleasantMissions = {
				"Have someone lick tahinee off a team member.",
				"Stand on a table in a restaurant and sing a song from beginning to end.",
				"Scare somebody from inside a trash can.",
				"Propose to a waitress"};
		String[] hardMissions = {
				"Trade places with a vendor and sell something of his.",
				"Get a kiss from a tourist.",
				"Have someone slap you with a fish. (Bonus points, on the bum)", };
		/*
		 * String[] missions = { "Have a boy and a girl switch clothes.",
		 * "Build a human pyramid.",
		 * "Get a piece of meat, put it in your pants and get a dog to chase you."
		 * , "Dress as a super-hero and help somebody.",
		 * "Organize a protest with 20 participants.",
		 * "Make two strangers argue.", "Play soccer with someone in a suit.",
		 * "Make a abbey road picture.", "Drink coffee from someone's mouth.",
		 * "Drive a cab.", "Take a picture in a police car.",
		 * "Clean a window in somebody's house." };
		 */
		Mission mission1 = new Mission(100, "Enter a public fountain",
				"Take a picture inside a public fountain.", game1);
		Task task1 = new TextTask("Answer this",
				"What are the molecules you are standing in?", mission1);
		((TextTask) task1).setCorrectAnswer("H2O");
		Task task2 = new PictureTask(
				"Take a picture inside a public fountain.",
				"You can splash around if you want to.", mission1);
		mission1.addTask(task1);
		mission1.addTask(task2);
		game1.addMission(mission1);
		mission1 = new Mission(100, "מצאו את החנות הראשונה של רמי לוי",
				"", game1);
		task1 = new TextTask("מאיזו שנה החנות הראשונה?",
				"", mission1);
		((TextTask) task1).setCorrectAnswer("1976");
		task2 = new PictureTask(
				"צלמו את החנות",
				"", mission1);
		mission1.addTask(task1);
		mission1.addTask(task2);
		game1.addMission(mission1);
		Mission tempMission = new Mission(100, "Easy Missions",
				"Complete each of these easy tasks for 100 points.", game1);
		for (String mission : easyMissions) {
			Task tempTask = new PictureTask(mission, "", tempMission);
			tempTask.setScore(100);
			tempMission.addTask(tempTask);
		}
		task2 = new BarcodeTask("Scan a QR code stuck behind bars",
				"Find the barcode on HaShikma street and scan it.", tempMission);
		// uncomment the next line and add the value for the QR code
		// ((BarcodeTask) task2).setBarcodeContents(contents);
		tempMission.addTask(task2);
		task2 = new BarcodeTask("Scan a QR code on a balcony",
				"Find the barcode on the fourth floor and scan it.", tempMission);
		// uncomment the next line and add the value for the QR code
		// ((BarcodeTask) task2).setBarcodeContents(contents);
		tempMission.addTask(task2);
		task2 = new BarcodeTask("Scan a QR code left on a car window",
				"The car was last seen near Mahane Yehuda.", tempMission);
		// uncomment the next line and add the value for the QR code
		// ((BarcodeTask) task2).setBarcodeContents(contents);
		tempMission.addTask(task2);
		task2 = new GPSTask(
				"Stand on a balcony in Nachlaot",
				"The balcony is on the second floor of a house, it has a black rail.",
				tempMission);
		((GPSTask) task2).setLatitude((int) (31.783127 * 10E6));
		((GPSTask) task2).setLongitude((int) (35.214375 * 10E6));
		tempMission.addTask(task2);
		game1.addMission(tempMission);
		tempMission = new Mission(
				100,
				"Unpleasant Missions",
				"These tasks are not as pleasant, completion is 200 points per task.",
				game1);
		for (String mission : unpleasantMissions) {
			Task tempTask = new PictureTask(mission, "", tempMission);
			tempTask.setScore(200);
			tempMission.addTask(tempTask);
		}
		game1.addMission(tempMission);
		tempMission = new Mission(
				100,
				"Hard Missions",
				"These tasks are pretty hard, so you get 400 points for each one.",
				game1);
		for (String mission : hardMissions) {
			Task tempTask = new PictureTask(mission, "", tempMission);
			tempTask.setScore(400);
			tempMission.addTask(tempTask);
		}
		game1.addMission(tempMission);

		user.addNewGame(game1);

		Game invite1 = new Game("Riddle me this!",
				"Answer this riddles and get some... points!",
				Calendar.getInstance(), false, Game.Visibility.PUBLIC);
		Mission invMission1 = new Mission(100, "Good old fashioned riddles",
				"Some would say these are classics.", invite1);
		Task invTask = new TextTask(
				"Inside/outside/inside/outside",
				"You throw away the outside and cook the inside. Then you eat the outside and throw away the inside. What did you eat?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("corn");
		invMission1.addTask(invTask);
		invTask = new TextTask("Some word-play",
				"What can you catch but not throw?", invMission1);
		((TextTask) invTask).setCorrectAnswer("a cold");
		invMission1.addTask(invTask);
		invTask = new TextTask(
				"Location, location, location.",
				"I can run but not walk. Wherever I go, thought follows close behind. What am I?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("a nose");
		invMission1.addTask(invTask);
		invTask = new TextTask("Small world traveler",
				"What goes around the world but stays in a corner?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("a stamp");
		invMission1.addTask(invTask);
		invTask = new TextTask(
				"Odd feeding habits",
				"Give me food, and I will live; give me water, and I will die. What am I?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("fire");
		invMission1.addTask(invTask);

		invite1.addMission(invMission1);

		invMission1 = new Mission(
				100,
				"Groaners",
				"Be careful with these tricky word problems. All is not what it seems!",
				invite1);
		invTask = new TextTask(
				"Farmers market",
				"A farmer had nine sheep, and all but seven died. How many did he have left?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("seven");
		invMission1.addTask(invTask);
		invTask = new TextTask(
				"Bus driving",
				"You are a bus driver. At the first stop of the day, eight people get on board. At the second stop, four get off, and eleven get on. At the third stop, two get off, and six get on. At the fourth stop, thirteen get off, and one gets on. At the fifth stop, five get off, and three get on. At the sixth stop, three get off, and two get on. What color are the bus driver's eyes?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("your eye color");
		invMission1.addTask(invTask);
		invTask = new TextTask(
				"Apples",
				"If you take two apples from three apples, how many do you have?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("two");
		invMission1.addTask(invTask);
		invTask = new TextTask(
				"Five letter word",
				"A certain five letter word becomes shorter when you add two letters to it. What is the word?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("short");
		invMission1.addTask(invTask);
		invTask = new TextTask(
				"Months",
				"Some months have 30 days. Some months have 31 days. How many months have 28 days?",
				invMission1);
		((TextTask) invTask).setCorrectAnswer("12");
		invMission1.addTask(invTask);
		invite1.addMission(invMission1);
		user.addNewGame(invite1);

		return user;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.about:
			startActivity(new Intent(this, About.class));
			return true;
		}
		return false;
	}
}